﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using MiniStock.BusinessLogic.EventQueue.Queue;
using MiniStock.Entities.Common;
using MiniStock.Entities.Common.Events;
using System.Collections.Generic;
using MiniStock.Entities.Stock.Events;
using MiniStock.Entities.Stock.Events.Customer;

namespace MiniStock.BusinessLogic.EventDispatcher.Test
{
    
    
    /// <summary>
    ///This is a test class for MsmqQueueTest and is intended
    ///to contain all MsmqQueueTest Unit Tests
    ///</summary>
    [TestClass()]
    public class MsmqQueueTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            SerializationHelper.KnownEventTypes.Clear();
            SerializationHelper.RegisterKnownEventTypes(new[] {typeof(CustomerEventBase)});
            EventBaseExtender.InitSerializer();
        }
        
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

                readonly Guid[] CustomerId = new[] {
            Guid.NewGuid(),
            Guid.NewGuid(),
            Guid.NewGuid(),
            Guid.NewGuid(),
        };

        readonly Guid[] EventId = new[] {
            Guid.NewGuid(),
            Guid.NewGuid(),
            Guid.NewGuid(),
            Guid.NewGuid(),
            Guid.NewGuid(),
            Guid.NewGuid(),
            Guid.NewGuid(),
            Guid.NewGuid(),
        };

        /// <summary>
        ///A test for CleanupClientAffinity
        ///</summary>
        [TestMethod()]
        public void CleanupClientAffinityTest()
        {
        }

        /// <summary>
        ///A test for CollectEvents
        ///</summary>
        [TestMethod()]
        public void CollectEventsTest()
        {
        }

        /// <summary>
        ///A test for Dequeue
        ///</summary>
        [TestMethod()]
        public void DequeueTest()
        {
            var sut = new MsmqQueue { Address = @".\private$\MiniStock.1", };
            sut.Enqueue(new CreateCustomerEvent { EventId = EventId[0], EventDate = DateTime.Now.AddMilliseconds(0), Id = CustomerId[0], Name="Teszt ügyfél 1"});

            sut = new MsmqQueue { Address = @".\private$\MiniStock.1", };
            var result = sut.Dequeue();

            Assert.IsNotNull(result);
            Assert.AreEqual(EventId[0], result.EventId);
            Assert.AreEqual("Teszt ügyfél 1", ((CreateCustomerEvent)result).Name);
        }

        /// <summary>
        ///A test for Dequeue
        ///</summary>
        [TestMethod()]
        public void DequeueTest_Empty()
        {
            var sut = new MsmqQueue { Address = @".\private$\MiniStock.1", };
            var result = sut.Dequeue();

            Assert.IsNull(result);
        }

        /// <summary>
        ///A test for HasEvent
        ///</summary>
        [TestMethod()]
        public void HasEventTest()
        {
            var sut = new MsmqQueue { Address = @".\private$\MiniStock.1", };
            Assert.AreEqual(false, sut.HasEvent());

            sut.Enqueue(new CreateCustomerEvent { EventId = EventId[0], EventDate = DateTime.Now.AddMilliseconds(0), Id = CustomerId[0], Name = "Teszt ügyfél 1" });
            Assert.AreEqual(true, sut.HasEvent());
            var result = sut.Dequeue();
            Assert.AreEqual(false, sut.HasEvent());
        }
    }
}
